const express = require('express')
const app = express()
const bodyParser = require('body-parser')

/**
 * application/json
 *
 * http://127.0.0.1/api/post
 *  ● { "name": "张智琦", "age": 18 }
 */
app.post('/api/post', bodyParser.json(), (req, res) => {
  console.log(req.body)
  res.send({
    status: 0,
    msg: '成功',
    data: req.body
  })
})

/**
 * application/x-www-form-urlencoded
 *
 * http://127.0.0.1/api/post2
 *  ● name  张智琦
 *  ● age   18
 */
app.post(
  '/api/post2',
  bodyParser.urlencoded({ extended: false }),
  (req, res) => {
    console.log(req.body)
    res.send({
      status: 0,
      msg: '成功',
      data: req.body
    })
  }
)

app.listen(80, () => {
  console.log('Server running at http://127.0.0.1')
})
